package com.iwop.moto.controller;

import com.iwop.moto.auth.LoginAuth;
import com.iwop.moto.comm.ErrCode;
import com.iwop.moto.model.ResultModel;
import com.iwop.moto.model.SuperRecordModel;
import com.iwop.moto.service.MGoodsService;
import com.iwop.moto.service.MTrademarkService;
import com.iwop.moto.utils.stringUtil.SequenceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2016/8/24.
 */
@Controller
public class MTrademarkController extends BaseController {

    @Autowired
    MTrademarkService mTrademarkService;

    /**
     * ****************************
     * 功能:商品品牌 - 新增
     * gw 2016-8-25 10:13:02
     * 参数:tid 品牌ID
     * 参数:user_id 用户ID
     * 参数:pictrue 商标图片
     * 参数:name 商标名称
     * 参数:status_flag 删除标识
     * 参数:status_time 修改时间
     */
    @RequestMapping("/api/trademark/add")
    @ResponseBody
    public ResultModel trademarkAdd() {
        SuperRecordModel SRModel = this.getSRModel();
        try {
            String tid = UUID.randomUUID().toString();
            SRModel.put("tid", tid);
            SRModel.put("status_time", new Date());
            SRModel.put("status_flag", "1");
            SRModel.put("sort", "0");
            mTrademarkService.insertTrademark(SRModel);
            return getSucceed(ErrCode.SUCCEED);
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     * ****************************
     * 功能:商品品牌 - 修改
     * gw 2016-8-25 10:13:02
     * 参数:tid 品牌ID
     * 参数:pictrue 商标图片
     * 参数:name 商标名称
     * 参数:status_time 修改时间
     */
    @RequestMapping("/api/trademark/edit")
    @ResponseBody
    public ResultModel trademarkEdit() {
        SuperRecordModel SRModel = this.getSRModel();
        try {
            SRModel.put("status_time", new Date());
            mTrademarkService.upTrademark(SRModel);
            return getSucceed(ErrCode.SUCCEED);
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     * 功能:搜索商品品牌
     * param name 商品品牌名称
     * param user_id 用户Id
     */
    @RequestMapping("/api/trademark/get")
    @ResponseBody
    public ResultModel getTrademark() {
        SuperRecordModel SRModel = this.getSRModel();
        try {
            List list = mTrademarkService.getTrademark(SRModel);
            return getSucceed(list, ErrCode.SUCCEED);
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     * 功能:删除商品品牌
     * param tid 商品品牌id
     * param user_id 用户id
     */
    @RequestMapping("/api/trademark/delete")
    @ResponseBody
    public ResultModel deleteTrademark() {
        SuperRecordModel SRModel = this.getSRModel();
        try {
            SRModel.put("statust_ime", new Date());
            SRModel.put("status_flag", "0");
            mTrademarkService.deleteTrademark(SRModel);
            return getSucceed(ErrCode.SUCCEED);
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     * 添加我的摩店的品牌
     */
    @RequestMapping("/api/trademark/detail/add")
    @ResponseBody
    @LoginAuth(isval = true)
    public ResultModel addTrademarkDetail(HttpServletRequest request) {
        SuperRecordModel SRModel = this.getSRModel();
        try {
            String tradeIds = SRModel.getString("trade_id");
            String userId = SRModel.getString("session_id");
            String[] tradeIdArr = tradeIds.split(",");
            for (String trade : tradeIdArr) {
                SuperRecordModel srm = new SuperRecordModel();
                srm.put("user_id", userId);
                srm.put("trade_id", trade);
                srm.put("sid", UUID.randomUUID().toString());
                mTrademarkService.insertTrademarkDetail(srm);
            }
            return getSucceed(ErrCode.SUCCEED);
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     * 修改我的摩店的品牌
     */
    @RequestMapping("/api/trademark/detail/edit")
    @ResponseBody
    @LoginAuth(isval = true)
    public ResultModel editTrademarkDetail(HttpServletRequest request) {
        SuperRecordModel SRModel = this.getSRModel();
        try {
            mTrademarkService.deleteTrademarkDetailByUserId(SRModel.getString("session_id"));
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
        ResultModel resultModel = addTrademarkDetail(request);
        return resultModel;
    }

    /**
     * 获取我的摩店的品牌
     */
    @RequestMapping("/api/trademark/detail/get")
    @ResponseBody
    @LoginAuth(isval = true)
    public ResultModel getTrademarkDetail(HttpServletRequest request) {
        SuperRecordModel SRModel = this.getSRModel();
        try {
            SRModel.put("user_id", SRModel.getString("session_id"));
            List<SuperRecordModel> list = mTrademarkService.getTrademarkDetail(SRModel);
            return getSucceed(list, ErrCode.SUCCEED);
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }
}
